/**
 * 张志浩(作者)
 * 2018-05-16(开始日期)
 * 2018--(最终更改日期)
 * xxx(最后更改者)
 * */
/* 获取三十天内的日期 */
var d = new Date();
var weekday = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
var month, myddy, day, r; //month当前月份，myddy当前星期几，day当前日期
var content = "";
for (var i = 1; i < 31; i++) {
  r = d.getDate();
  day = d.getDate() < 10 ? '0' + d.getDate() : d.getDate(); //获取存储当前日期
  myddy = d.getDay(); //获取存储当前星期
  r < 0 ? month = d.getMonth() < 10 ? '0' + d.getMonth() : d.getMonth() : month = d.getMonth() + 1 < 10 ? '0' + parseInt(d.getMonth() + 1) : d.getMonth() + 1; //获取存储当前月份

  if (i == 1) {
    content += '<li class="nav_active today" data-id="' + month + '月' + day + '日(今天)"><p>' + "今天" + '</p>';
    content += '<p>' + month + '-' + day + '</p></li>';
  } else if (i == 2) {
    content += '<li data-id="' + month + '月' + day + '日(明天)"><p>' + "明天" + '</p>';
    content += '<p>' + month + '-' + day + '</p></li>';
  } else {
    content += '<li data-id="' + month + '月' + day + '日(' + weekday[myddy] + ')"><p>' + weekday[myddy] + '</p>';
    content += '<p>' + month + '-' + day + '</p></li>';
  }
  r = d.getDate() + 1; //日期加1
  d.setDate(Math.abs(r)); //重新设置当前时间
}
$("#main_nav").append(content) //回显

/* 时间序列 */
// 今天
function render_today() {
  var hour = d.getHours(); //获取当前小时数(0-23)
  var min = d.getMinutes(); //获取当前分钟数(0-59)
  if (min >= 30) {
    render_time(hour + 1, 0)
  } else {
    render_time(hour, min)
  }
}
render_today()

/* 渲染时间序列 */
function render_time(h, m) {
  var minute = ["00", "30"];
  var time_list = "",
    class_name = "";
  for (var i = 9; i <= 19; i++) {
    if (i < h) {
      class_name = "t_disabled";
    } else {
      class_name = "";
    }
    if (i == 19) {
      time_list += "<li class='" + class_name + "' data-id='" + i + ":" + minute[0] + "'><p>" + i + ":" + minute[0] + "</p></li>"
      break;
    }
    for (var j = 0; j < 2; j++) {

      if (i == h && m > 0 && j == 0) {
        console.log(i + "--" + h + "--" + m + "--" + j)
        class_name = "t_disabled";
      } else if (i == h) {
        class_name = "";
      }
      time_list += "<li class='" + class_name + "' data-id='" + i + ":" + minute[j] + "'><p>" + i + ":" + minute[j] + "</p></li>"
    }
  }
  $("#time_list").html(time_list);
}
/* 导航点击事件 */
$("#main_nav").on("click", "li", function() {
  $(this).addClass("nav_active").siblings().removeClass("nav_active");
  var week = $(this).attr("data-id")
  $("#time_temp").val(week)
  if ($(this).hasClass("today")) {
    render_today() //今天
  } else {
    render_time(9, 0) //其他天
  }
})
/* 时间序列点击事件 */
$("#time_list").on("click", "li", function() {
  var disabled = $(this).hasClass("t_disabled");
  var active = $(this).hasClass("t_active");
  if (disabled || active) {
    return;
  }
  var val = $("#time_temp").val();
  val = val + $(this).attr("data-id");
  $("#time_temp").val(val)
  $(this).addClass("t_active").siblings().removeClass("t_active");
})
$("#sure_time").click(function() {
  var day = $("#main_nav").find(".nav_active").attr("data-id");
  var h_m = $("#time_list").find(".t_active").attr("data-id");
  if (!(day && h_m)) {
    $.toast("请选择服务时间");
    return
  }
  $("#service_time").html(day + h_m)
  $.closeModal(".popup-services")
})
$.init();